package com.jphoebe.plugin.mybatis.service.setting;

import com.intellij.openapi.components.*;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.util.xmlb.XmlSerializerUtil;
import com.jphoebe.plugin.mybatis.bean.setting.GlobalSetting;
import com.jphoebe.plugin.mybatis.service.JavaService;
import org.jdom.Element;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * 全局配置
 *
 * @author 蒋时华
 * @date 2019/2/19
 */
@State(name = "JPMybatisGlobalSetting",
        storages = {
                @Storage("JPMybatis/JPMybatisGlobalSetting.xml")
        }
)
public class GlobalSettingService implements PersistentStateComponent<GlobalSetting> {

    private GlobalSetting globalSetting;

    public static GlobalSettingService getInstance() {
        return ServiceManager.getService(GlobalSettingService.class);
    }

    public GlobalSettingService() {
        this.globalSetting = new GlobalSetting();
    }

    @Nullable
    @Override
    public GlobalSetting getState() {
        if (globalSetting == null) {
            globalSetting = new GlobalSetting();
        }
        return globalSetting;
    }

    @Override
    public void loadState(@NotNull GlobalSetting state) {
        XmlSerializerUtil.copyBean(state, globalSetting);
    }

    public void setNaviJumpStatus(boolean status) {
        globalSetting.setNaviJumpStatus(status);
        this.loadState(globalSetting);
    }

    public void setNaviJumpIcon(Integer status) {
        globalSetting.setNaviJumpIcon(status);
        this.loadState(globalSetting);
    }

    public void setLanguage(Integer status) {
        globalSetting.setLanguage(status);
        this.loadState(globalSetting);
    }

}
